var toContent;
require(['grid', 'topBar', 'validator'], function () {
	var //editDialog,
    $top = $('#top'),
    $grid = $('#grid');
	var userInfo = commonFun.execParentMethod("getUserInfo");
	
	var list = window.top.document.getElementById("noticeList");
	var $noticeList = $(list);
	//初始化顶部搜索栏
	function topBarInit() {
        $top.topBar({
            left: [
                {
                    text: '标题',
                    name: 'announTitle',
                    width: '140',
                    type: 'text'
                },
                
                {
				    text: '查询',
				    type: 'btn',
//				    authority: '',
				    click: function () {
				    	reloadGridData();
				    }
				}
            ],
            right: [
				{
                    text: '删除',
                    type: 'btn',
                    width: '698',
//                    authority: '',
                    click: function () {
                    	deleteDoc();
                    }
                },
                {
                    text: '+添加',
                    type: 'btn',
                    width: '698',
//                    authority: '',
                    click: function () {
                    	$("#saveAnnounceForm").reset();
                    	$("#updateUser").val(userInfo.acctName);
                    	$("#showList").hide();
                    	$("#edit").show();
                    	$("#content").hide();
                    }
                }
            ]
        });
    }
	
	//显示列表
	$("#returnList_e, #returnList_c").click(function(){
		$("#showList").show();
    	$("#edit").hide();
    	$("#content").hide();
    	reloadGridData();
	});
	//保存按钮绑定
	$("#saveDoc").click(function(){
		if ($.validator('#saveAnnounceForm')) {
			saveAnnounce();
		}
	});
	//删除公告绑定
	$("#deleteDoc").click(function(){
		deleteSingleDoc($("#hid_announId").val());
		reloadGridData();
		$("#showList").show();
		$("#edit").hide();
		$("#content").hide();
	});
	
	//初始化列表
	function gridInit() {
        $grid.grid({
            url: "/SystemAnnounce/getSystemAnnounce.htm",
            multiselect: true,
            colModel: [
                {name: "announId", index: "announId", label: 'announId', key: true, hidden: true},
                {name: "readFlag", index: "readFlag", label: 'readFlag', key: true, hidden: true},
                {name: "announTitle", index: "announTitle", label: '标题',
                	formatter:function(cellValue, options, rowObject){
                		return "<a onclick='toContent(\""+rowObject.announId+"\")'>"+cellValue+"</a>";
                	}
                },
                {name: "announTime", index: "announTime", label: '时间'},
                {name: "updateUser", index: "updateUser", label: '发布者'},
                {name: "isRead", width: 90, label: '是否已读', title: false,
                    formatter: function(cellValue, options, rowObject){
                    	if(rowObject.readFlag=='1')
                    		return "<span class='operate operate-noread'>已读</span>";
                    	else
                    		return "<span class='operate operate-read'>未读</span>";
                	}
                },
                {name: "operate", width: 90, label: '操作', title: false,
                    formatter: 'operArea',
                    formatoptions: [{
                        text: '删除',
                        css:'operate-delete',
                        click: function (data) {
                            $.messager.confirm('删除', '确定要删除该条记录吗？', 'delete', function (r) {
                                if (r) {
                                	deleteSingleDoc(data.announId);
                                }
                            });
                        }
                    }]
                }
            ],
            shrinkToFit:true,
            width:$(window).width() - 30,
			height:$(window).height()-131
            
        });
    }
	
	//显示正文
	toContent = function(docId){
		$("#hid_announId").val(docId);
		$.ajaxExt({
    		url:"/SystemAnnounce/getAnnounceContent.htm",
    		data:{"announId":docId}
    	}).done(function(data){
    		$("#docTtile").html("<h2>"+data.announTitle+"</h2>");
    		$("#docAuthor").html("<h3>"+data.updateUser+"<span> | </span>"+data.announTime+"</h3>");
    		$("#docContent").html("<p>"+data.announCntnt+"</p>");
    	});
		
		$.ajaxExt({
			url:"/SystemAnnounce/saveReadAnnounce.htm",
			data:{"announId":docId}
		}).done(function(result){
		});
		
		$("#showList").hide();
    	$("#edit").hide();
    	$("#content").show();
	};
	
	//刷新列表
	function reloadGridData(){
		$grid.grid('setGridParam', {
            datatype: "JSON",
            page: 1,
            postData: $top.getValues()
        }).trigger("reloadGrid");
	}
	
	//保存公告
	function saveAnnounce(){
		var loadMask = $.createMask();
		$("#announcer").val(userInfo.acctName);
		$.ajaxExt({
			async:true,
			url:'/SystemAnnounce/addSystemAnnounce.htm',
			data:{
				"announTitle":$("#announTitle").val(),
				"updateUser":userInfo.acctName,
				"announCntnt":$("#announCntnt").val()
			}
		}).done(function(){
			loadMask.remove();
			$.messager.alert("提示", "添加成功!", "success");
			reloadGridData();
			$("#showList").show();
			$("#edit").hide();
			$("#content").hide();
			loadNoticeList();
		});
	}
	
	//删除公告
	function deleteSingleDoc(announId){
		$.ajaxExt({
			async:true,
    		url : "/SystemAnnounce/deleteSystemAnnounce.htm",
			data : {'docIds' : announId}
		}).done(function(){
			$.messager.alert("提示","您成功删除一条公告。","success");
			reloadGridData();
			loadNoticeList();
		});
	}
	
	function loadNoticeList(){
    	$.ajaxExt({
            url: "/logon/getNoticeList.htm"
        }).done(function (data) {
        	var anime = true;
        	var content = $noticeList.html();
        	console.info(content);
        	if(content && content.trim() != '' ){
        		anime = false;
        	}
        	console.info(anime);
            var html = "";
            $noticeList.empty();
            if (data && data.length > 0) {
                $.each(data, function (index, obj) {
                    html += '<p><a data-val="{0}">{1}</a></p>'.format(obj.announId, obj.announTitle);
                });
            }
            $noticeList.html(html);
            if(anime){
            	noticeAnimate();
            }
        });
    }
	
	
	
	
	function noticeAnimate() {

        function move() {
            $noticeList.animate(
                {"marginTop": -20 + "px"},
                600,
                function () {
                    $noticeList.css({marginTop: 0}).find("p:first").appendTo($noticeList); //appendTo能直接移动元素
                }
            );
        }

        function auto() {
            return setInterval(function () {
                move();
            }, 2600);
        }
        
        var t = auto();

        $noticeList.hover(function () {
            clearInterval(t);
        }, function () {
            t = auto();
        });
    }
	
	
	
	//批量删除公告
	function deleteDoc(){
		var docIds = $grid.grid('getGridParam', 'selarrrow');
		if(docIds == ""){
			$.messager.alert("提示", "请至少选择一条公告!");
		}else{
			$.ajaxExt({
				url:"/SystemAnnounce/deleteSystemAnnounce.htm",
				data:{"docIds":docIds.toString()}
			}).done(function(){
				$.messager.alert("提示", "删除成功!", "info");
				reloadGridData();
			});
		}
	}
	
	gridInit();
	topBarInit();
});